package com.educoart.portlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import com.educoart.util.IConstants;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Organization;
import com.liferay.portal.model.User;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.slayer.service.BridgeLocalServiceUtil;
import com.slayer.service.InvitationLocalServiceUtil;

public class RegisterPortlet extends MVCPortlet {
	public void serveResource(ResourceRequest resourceRequest,
			ResourceResponse resourceResponse) throws IOException,
			PortletException {
		
		String cmd = ParamUtil.getString(resourceRequest, Constants.CMD);
		
		if (cmd.equalsIgnoreCase(IConstants.CMD_CHECK_DUPLICATE)) { 
			long companyId = PortalUtil.getCompanyId(resourceRequest);

			String emailAddress = ParamUtil.getString(resourceRequest, "emailAddress");
	        PrintWriter writer = resourceResponse.getWriter();
	        writer.println(BridgeLocalServiceUtil.isEmailExists(companyId, emailAddress));
	        writer.flush();
		}
    }
	
	public void register(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {

		// User fields
		String firstName = ParamUtil.getString(actionRequest, "firstName");
		String lastName = ParamUtil.getString(actionRequest, "lastName");
		String emailAddress = ParamUtil.getString(actionRequest, "emailAddress");
		boolean male = ParamUtil.getBoolean(actionRequest, "gender");
		
		Date dateOfBirth = ParamUtil.getDate(actionRequest, "dateOfBirth",
				new SimpleDateFormat("MM/dd/yyyy"));
		long companyId = PortalUtil.getCompanyId(actionRequest);

		
		ServiceContext serviceContext = null;
		try {
			serviceContext = ServiceContextFactory.getInstance(actionRequest);
		} catch (PortalException e) {
			e.printStackTrace();
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		String mobileNumber = ParamUtil.getString(actionRequest, "mobileNumber");
		
		User user = RegistrationHelper.createUser(companyId, emailAddress,
				firstName, lastName, male, mobileNumber, dateOfBirth,
				serviceContext);
		
		checkInvitationStatus(actionRequest, emailAddress);
		
		// Organization Creation
		if (ParamUtil.getBoolean(actionRequest, "org", false)) {
			String organizationName = ParamUtil.getString(actionRequest, "organizationName");
			String website = ParamUtil.getString(actionRequest, "website");
			String subdomain = ParamUtil.getString(actionRequest, "subdomain");
			
			Organization organization = RegistrationHelper.createOrganization(companyId, user.getUserId(),
					organizationName, website, subdomain,
					serviceContext);
			
			OrgSiteHelper.setOrganizationSite(companyId, organization.getOrganizationId(), "Community Site", subdomain, false);
			OrgSiteHelper.setOrganizationSite(companyId, organization.getOrganizationId(), "Intranet Site", subdomain, true);			
		}
		
		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
				.getAttribute(WebKeys.THEME_DISPLAY);
		actionResponse.sendRedirect(themeDisplay.getURLSignIn());
	}
	
	private void checkInvitationStatus(PortletRequest portletRequest, String emailAddress) {
		// check for invitation Id in the request
		long invitationId = ParamUtil.getLong(portletRequest, "invitationId", 0l);
		long inviterId = ParamUtil.getLong(portletRequest, "inviterId", 0l);
		
		if (invitationId > 0l) {
			InvitationLocalServiceUtil.updateInviation(invitationId, emailAddress);
		} else if (inviterId > 0l) {
			InvitationLocalServiceUtil.initInvitation(inviterId, emailAddress, 
					IConstants.INVITE_JOINED);
		}
	}	
}